package org.finir.fakturisanje.rich.web.auth;

import org.finir.fakturisanje.business.service.KontrolerAL;
import org.finir.fakturisanje.persistence.Preduzece;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

@Name("authenticator")
public class Authenticator {
	@Logger
	Log log;

	@In
	Identity identity;

	@In
	Credentials credentials;

	@In(create = true)
	@Out(required = false)
	Preduzece preduzeceLoggedIn;

	@In("#{kontrolerAL}")
	KontrolerAL kontrolerAL;

	public boolean authenticate() {
		log.info("authenticating {0}", credentials.getUsername());
		preduzeceLoggedIn.setUsername(credentials.getUsername());
		preduzeceLoggedIn.setPassword(credentials.getPassword());
		Preduzece pred = kontrolerAL.logovanjePreduzeca(preduzeceLoggedIn);
		preduzeceLoggedIn = pred;

		if (preduzeceLoggedIn == null) {
			return false;
		} else {
			return true;
		}
	}
}
